package com.hhsjtest.segiui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

public class RCLayout extends FrameLayout {

    Path path;
    Paint paint;
    RectF reactF;

    float[] radii;

    public RCLayout(Context context) {
        this(context, null);
    }

    public RCLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RCLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        path = new Path();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        reactF = new RectF(0f, 0f, 0F, 0F);
        radii = new float[]{100f, 100f, 0f, 0f, 100f, 100f, 50f, 50f};
        setWillNotDraw(false);
    }


    @Override
    public void draw(Canvas canvas) {
        beforeDraw(canvas);
        super.draw(canvas);
        canvas.restore();
    }

    private void beforeDraw(Canvas canvas) {
        canvas.save();
        path.reset();
        reactF.left = getPaddingLeft();
        reactF.top = getPaddingTop();
        reactF.right = getWidth() - getPaddingRight();
        reactF.bottom = getHeight() - getPaddingBottom();
        path.addRoundRect(
                reactF,
                radii,
                Path.Direction.CW);
        canvas.clipPath(path);
    }

    public void setRadii(float[] radii) {
        this.radii = radii;
    }

}
